package com.example.testrecorder;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.testrecorder.domain.User;
import com.example.testrecorder.utils.Des3Util;
import com.example.testrecorder.utils.LogUtil;
import com.example.testrecorder.utils.MyDialog;

import org.litepal.FluentQuery;
import org.litepal.LitePal;

import java.util.List;

public class LoginActivity extends BaseActivity implements View.OnClickListener{
    private EditText accountEt,passwordEt;
    private LinearLayout registerLl;
    private CheckBox rememberPasCb;
    private Button loginBtn;
    private TextView registerTv,findPasTv;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private static final String TAG = "LoginActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        registerLl=findViewById(R.id.registerLl);
        registerLl.setOnClickListener(this);
        sharedPreferences=getSharedPreferences("user",MODE_PRIVATE);
        editor=sharedPreferences.edit();
        accountEt=findViewById(R.id.accountEt);
        accountEt.setText(sharedPreferences.getString("userName",""));
        passwordEt=findViewById(R.id.passwordEt);
        passwordEt.setText(Des3Util.decrypt("lyy",sharedPreferences.getString("password","")));
        rememberPasCb=findViewById(R.id.rememberPasCb);
        rememberPasCb.setChecked(!TextUtils.isEmpty(sharedPreferences.getString("userName", "")));
        loginBtn=findViewById(R.id.loginBtn);
        loginBtn.setOnClickListener(this);
        registerTv=findViewById(R.id.registerTv);
        registerTv.setOnClickListener(this);
        findPasTv=findViewById(R.id.findPasTv);
        findPasTv.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.loginBtn:
                String account = accountEt.getText().toString().trim();
                String password = passwordEt.getText().toString().trim();
                if (TextUtils.isEmpty(account)){
                    Toast.makeText(LoginActivity.this,"登录失败，账号不能为空！",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(password)){
                    Toast.makeText(LoginActivity.this,"登录失败，密码不能为空！",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(rememberPasCb.isChecked()){//如果勾选了记住密码
                    editor.putString("userName",account);
                    editor.putString("password",Des3Util.encrypt("lyy",password));
                    editor.commit();
                }else {//如果没有勾选记住密码
                    editor.putString("userName","");
                    editor.putString("password","");
                    editor.commit();
                }
                List<User> users2 = LitePal.where("userName=? and password=?", account, Des3Util.encrypt("lyy",password)).find(User.class);
                if(users2.size()>0){
                    startActivity(new Intent(LoginActivity.this,MainActivity.class));
                    Toast.makeText(LoginActivity.this,"恭喜您，登陆成功！",Toast.LENGTH_SHORT).show();
                    MyApplication.currUser=users2.get(0);//将当前用户存入全局变量中
                }else {
                    Toast.makeText(LoginActivity.this,"登录失败，用户名或密码错误！",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.registerTv:
                startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
                break;
            case R.id.findPasTv:
                startActivity(new Intent(LoginActivity.this,FindPasswordActivity.class));
                break;
            case R.id.registerLl:
                startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
                break;
            default:
        }
    }
}